package icu.wdtt.mapper;


import icu.wdtt.model.Files;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface FilesMapper {

    @Select("SELECT f.* FROM (SELECT * FROM user_files WHERE uid=#{uid}) uf, files f WHERE f.fid=#{fid} AND uf.fid = f.fid")
    public Files getByUidAndFid(@Param("uid") int uid,@Param("fid") int fid);

    @Select("SELECT * FROM files WHERE fid=#{fid}")
    public Files getByFid(int fid);

    @Select("SELECT * FROM files")
    public List<Files> getAll(int uid);

    @Insert("INSERT INTO files VALUES(null,#{fname},#{furl},#{fopen},#{fdescribe},#{ftype},#{fmd5})")
    @Options(useGeneratedKeys = true,keyColumn = "fid",keyProperty = "fid")
    public void add(Files files);

    @Delete("DELETE FROM files WHERE fid=#{fid}")
    public void del(int fid);

    @Update("UPDATE files SET fname=#{fname} WHERE fid=#{fid}")
    public void modifyFilesName(@Param("fid") int fid,@Param("fname") String fname);

    @Insert("INSERT INTO user_files SET fid=#{fid},uid=#{uid}")
    public void addFilesAndUser(@Param("fid") int fid , @Param("uid") int uid);

    @Delete("DELETE FROM user_files WHERE fid=#{fid}")
    public void delFilesAndUser(@Param("fid") int fid);

    @Select("SELECT fid FROM user_files WHERE fid=#{fid} AND uid=#{uid}")
    public Integer isFilesByUser(int fid,int uid);

    @Select("SELECT f.* FROM (SELECT * FROM user_files WHERE uid=#{uid}) uf,files f WHERE uf.fid=f.fid AND fname LIKE '%${fname}%' ")
    public List<Files> getByRname(@Param("fname") String fname, @Param("uid") int uid);


    @Select("SELECT f.* FROM (SELECT * FROM user_files WHERE uid = #{uid}) uf, files f WHERE uf.fid = f.fid")
    public List<Files> getMainFiles(int uid);

    @Select("SELECT * FROM files WHERE fmd5 = #{fmd5}")
    public Files getFilesByMd5(String fmd5);
}
